package com.album.activity.pageView;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

import com.album.R;
import com.album.activity.adapter.PhotoAdapter; 

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.TranslateAnimation;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;
 

public class PhotoView {

	private RelativeLayout layout;
	
	private RelativeLayout layout1;
	private RelativeLayout layout2;
	private RelativeLayout layout3;
	
	private TextView tab1,tab2,tab3; 
	
	private ImageView first;
	private int current = 1; // 默认选中第一个，可以动态的改变此参数值
	private int selectedView =0;
	private ViewFlipper my_viewflipper;
	private PhotoAdapter adapter;
	private GridView  gridView;
	private List<SoftReference<Bitmap>> listBitmap;
	
	private Context context;
	private View view;		//总视图 
	private boolean isAdd = false; // 是否添加过 top_select
	private int select_width; // top_select_width
	private int select_height; // top_select_height
	private int firstLeft; // 第一次添加后的左边距*****
	private int startLeft; // 起始左边距
	private LinearLayout  linearLayout;
	
	public PhotoView(Context context,int layoutId,LinearLayout linearLayout){
		this.context = context; 
		this.view = LayoutInflater.from(context).inflate(layoutId, null);
		this.linearLayout = linearLayout;
		initUI();
	}
	private void initUI(){ 
		layout = (RelativeLayout)linearLayout.findViewById(R.id.root);
		
		layout1 = (RelativeLayout)linearLayout.findViewById(R.id.layout1);
		layout2 = (RelativeLayout)linearLayout.findViewById(R.id.layout2);
		layout3 = (RelativeLayout)linearLayout.findViewById(R.id.layout3);
		
		my_viewflipper = (ViewFlipper)linearLayout.findViewById(R.id.my_viewflipper);
		/********************************************************/
		gridView = (GridView)linearLayout.findViewById(R.id.gridView); 
		gridView.setGravity(Gravity.CENTER);// 位置居中 
		SoftReference<Bitmap> bitmap = adapter.drawableToBitmap(context.getResources().getDrawable(R.drawable.topbar_select));
		listBitmap = new ArrayList<SoftReference<Bitmap>>();
//		for(int i=0;i<10;i++){
//			listBitmap.add(bitmap);
//		} 
		adapter = new PhotoAdapter(context, listBitmap);
		/********************************************************/
		tab1 = (TextView)linearLayout.findViewById(R.id.tab1);
		tab1.setOnClickListener(onClickListener);
		tab2 = (TextView)linearLayout.findViewById(R.id.tab2);
		tab2.setOnClickListener(onClickListener);
		tab3 = (TextView)linearLayout.findViewById(R.id.tab3);
		tab3.setOnClickListener(onClickListener);
		
		RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		rl.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
		first = new ImageView(context);
		first.setTag("first");
		first.setImageResource(R.drawable.topbar_select);
		
		// 默认选中项
		switch (current) {
		case 1:
			layout1.addView(first, rl); 
			current = R.id.tab1;
			break;
		case 2:
			layout2.addView(first, rl);
			current = R.id.tab2;
			break;
		case 3:
			layout3.addView(first, rl);
			current = R.id.tab3;
			break;
		default:
			break;
		}
		adapter = new PhotoAdapter(context, listBitmap);
		gridView.setAdapter(adapter);
		my_viewflipper.setDisplayedChild(selectedView);
	}
	// 添加一个view，移除一个view
	private void replace() {
		switch (current) {
		case R.id.tab1:
			changeTop(layout1);
			break;
		case R.id.tab2:
			changeTop(layout2);
			break;
		case R.id.tab3:
			changeTop(layout3);
			break;
		default:
			break;
		}
	}
	
	private void changeTop(RelativeLayout relativeLayout){
		ImageView old = (ImageView) relativeLayout.findViewWithTag("first");;
		select_width = old.getWidth();
		select_height = old.getHeight();
		
		RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(select_width, select_height);
		rl.leftMargin = old.getLeft() + ((RelativeLayout)old.getParent()).getLeft();
		rl.topMargin = old.getTop() + ((RelativeLayout)old.getParent()).getTop();
		
		// 获取起始位置
		firstLeft = old.getLeft() + ((RelativeLayout)old.getParent()).getLeft();
		
		ImageView iv = new ImageView(context);
		iv.setTag("move");
		iv.setImageResource(R.drawable.topbar_select);
		
		layout.addView(iv , rl);
		relativeLayout.removeView(old);
	}
	
	private OnClickListener onClickListener = new OnClickListener(){
		public void onClick(View v) {
			if(!isAdd){
				replace(); // 初次使用移除old 添加新的top_select为RelativeLayout所使用
				isAdd = true;
			}
			
			ImageView top_select = (ImageView) layout.findViewWithTag("move");
			int tabLeft;
			int endLeft = 0;
			
			boolean run = false;

			switch (v.getId()) {
			case R.id.tab1:
				if (current != R.id.tab1) {
					// 中心位置
					tabLeft = ((RelativeLayout) tab1.getParent()).getLeft() + tab1.getLeft() + tab1.getWidth() / 2;
					// 最终位置
					endLeft = tabLeft - select_width / 2;
					current = R.id.tab1;
					run = true;
					selectedView = 0;
				}
				break;
			case R.id.tab2:
				if (current != R.id.tab2) {
					tabLeft = ((RelativeLayout) tab2.getParent()).getLeft() + tab2.getLeft() + tab2.getWidth() / 2;
					endLeft = tabLeft - select_width / 2;
					current = R.id.tab2;
					run = true;
					selectedView = 1;
				}
				break;
			case R.id.tab3:
				if (current != R.id.tab3) {
					tabLeft = ((RelativeLayout) tab3.getParent()).getLeft() + tab3.getLeft() + tab3.getWidth() / 2;
					endLeft = tabLeft - select_width/2;
					current = R.id.tab3;
					run = true;
					selectedView = 2;
				}
				break;
			default:
				break;
			}
			
			if(run){
				TranslateAnimation animation = new TranslateAnimation(startLeft, endLeft - firstLeft, 0, 0);
				startLeft = endLeft - firstLeft; // 重新设定起始位置
				animation.setDuration(300);
				animation.setFillAfter(true);
				top_select.bringToFront();
				top_select.startAnimation(animation);
				my_viewflipper.setDisplayedChild(selectedView);
			}
			Bitmap bitmap = adapter.drawableBitmap(context.getResources().getDrawable(R.drawable.topbar_select));
			listBitmap.add(new SoftReference<Bitmap>(bitmap));
			adapter.setListDataSetChanged(bitmap);
//			my_viewflipper.removeAllViews();
			System.out.println("select:"+selectedView);
			
		}

	};
	public View getView(){
		return view;
	}
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1: 
				break;
			case 2:  
				break;
			default:
			
				break;
			}
					
			}
	};
}
